package com.magic.zhixin.controller;

import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayUtil;
import com.magic.zhixin.config.WeixinConstant;
import com.magic.zhixin.entity.UserQuestion;
import com.magic.zhixin.entity.WeixinNotifyData;
import com.magic.zhixin.model.QuestionStatus;
import com.magic.zhixin.repository.UserQuestionRepository;
import com.magic.zhixin.service.WalletService;
import com.magic.zhixin.service.WeixinNotifyDataService;
import com.magic.zhixin.service.ZhiXinWenDaService;
import com.magic.zhixin.weixin.WeixinPayConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by liuhangjun on 2018/3/6.
 */
@Api(tags = "微信支付回调接口")
@RestController
@RequestMapping(value = "/api/wxpay")
public class WeixinPayNotifyController {

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private WeixinNotifyDataService weixinNotifyDataService;

	@Autowired
	private WalletService walletService;
//
//	@Autowired
//	private WeixinNotifyDataService weixinNotifyDataService;

	@ApiOperation(value = "微信支付商品结果回调地址")
	@PostMapping("/notify")
	public ResponseEntity<String> payGoodsNotify(@RequestBody String notifyData) throws Exception {

		logger.debug("Received weixin pay notify information of goods, notify data is: {}", notifyData);

		Map<String, String> returnMap = new HashMap<>();

		WeixinPayConfig config = new WeixinPayConfig();
		WXPay wxpay = new WXPay(config);

		Map<String, String> notifyMap = WXPayUtil.xmlToMap(notifyData); // 转换成map

		String payTimeEnd;
		String resultCode;
		String sn = "";
		String paymentNo;
		String totalFee;

		if (wxpay.isPayResultNotifySignatureValid(notifyMap)) {
			// 签名正确
			// 进行处理。
			// 注意特殊情况：订单已经退款，但收到了支付结果成功的通知，不应把商户侧订单状态从退款改成支付成功
			// 本小程序暂时不做退款业务处理
			payTimeEnd = notifyMap.get(WeixinConstant.PAY_TIME_END);
			resultCode = notifyMap.get(WeixinConstant.RESULT_CODE);
			sn = notifyMap.get(WeixinConstant.OUT_TRADE_NO);
			paymentNo = notifyMap.get(WeixinConstant.TRANSACTION_ID);
			totalFee = notifyMap.get(WeixinConstant.TOTAL_FEE);

			// 先将通知信息入库，再处理业务逻辑
			WeixinNotifyData weixinNotifyData = new WeixinNotifyData();
			weixinNotifyData.setOrderSn(sn);
			weixinNotifyData.setContent(notifyData);
			weixinNotifyDataService.add(weixinNotifyData);
//
			UserQuestion userQuestion = walletService.findUserQuestionBySn(sn);
			walletService.success(userQuestion,sn);
//			if (orderInfo == null) {
//				returnMap.put(WeixinConstant.RETURN_CODE, WeixinConstant.FAIL_STR);
//				returnMap.put(WeixinConstant.RETURN_MSG, "没有此订单信息");
//			} else if (OrderStatus.cancel.getCode() == orderInfo.getStatus()) {
//				returnMap.put(WeixinConstant.RETURN_CODE, WeixinConstant.FAIL_STR);
//				returnMap.put(WeixinConstant.RETURN_MSG, "订单已经取消");
//			} else if (OrderStatus.paid.getCode() == orderInfo.getStatus()) {
//				returnMap.put(WeixinConstant.RETURN_CODE, WeixinConstant.SUCCESS_STR);
//				returnMap.put(WeixinConstant.RETURN_MSG, "OK");
//			} else if (OrderStatus.unPay.getCode() == orderInfo.getStatus()) {

//				if (StringUtils.equals(resultCode, WeixinConstant.RESULT_CODE_FAIL)) {
//					returnMap.put(WeixinConstant.RETURN_CODE, WeixinConstant.RESULT_CODE_FAIL);
//					returnMap.put(WeixinConstant.RETURN_MSG, "微信订单通知处理结果失败，本系统做订单取消处理");
//
//					orderInfoService.cancelOrder(sn, CancelType.weixinPayFailedCancel, null, null);
//
//				} else {
//					// 微信订单处理成功，以下做业务处理
//					OrderInfo entity = new OrderInfo();
//					entity.setSn(sn);
//					entity.setPayType(PayType.weixin.getCode());
//					entity.setPayTime(DateUtils.parseDate(payTimeEnd));
//					entity.setPayNo(paymentNo);
//					entity.setPayMoney(AmountUtils.changeF2Y(Integer.parseInt(totalFee)));
//					orderInfoService.payOrder(entity);
//
//					returnMap.put(WeixinConstant.RETURN_CODE, WeixinConstant.SUCCESS_STR);
//					returnMap.put(WeixinConstant.RETURN_MSG, "OK");
//				}

//			} else {
//				returnMap.put(WeixinConstant.RETURN_CODE, WeixinConstant.FAIL_STR);
//				returnMap.put(WeixinConstant.RETURN_MSG, "订单处理其他错误");
//			}

		} else {
			UserQuestion userQuestion = walletService.findUserQuestionBySn(sn);
			walletService.fail(userQuestion,sn);
			// 签名错误，如果数据里没有sign字段，也认为是签名错误
			returnMap.put(WeixinConstant.RETURN_CODE, WeixinConstant.FAIL_STR);
			returnMap.put(WeixinConstant.RETURN_MSG, "签名校验失败");

		}

		return ResponseEntity.ok(WXPayUtil.mapToXml(returnMap));
	}
}
